package model

import (
	"gorm.io/gorm"
)

func AddCollection(db *gorm.DB, c Collection) (*Collection, error) {
	// validate collection schema definition

	result := db.Create(&c)
	if result.Error != nil {
		return nil, result.Error
	}
	return &c, nil
}

func ToggleCollection(db *gorm.DB, id uint, enable bool) error {
	result := db.Model(&Collection{ID: id}).Update("enable", enable)
	return result.Error
}

func UpdateCollectionComment(db *gorm.DB, id uint, comment string) error {
	result := db.Model(&Collection{ID: id}).Update("comment", comment)
	return result.Error
}

func DeleteCollection(db *gorm.DB, id uint) error {
	result := db.Delete(&Collection{}, id)
	return result.Error
}

func ListCollection(db *gorm.DB) ([]Collection, error) {
	var ret []Collection
	result := db.Find(&ret)
	if result.Error != nil {
		return nil, result.Error
	}
	return ret, nil
}

func GetCollection(db *gorm.DB, id uint) (*Collection, error) {
	var ret Collection
	result := db.First(&ret, id)
	if result.Error != nil {
		return nil, result.Error
	}
	return &ret, nil
}
